package com.zhuchaooasystem.utils;

/**
 * Created by CaoAnyang on 2016/6/3.
 */
public class DoubleUtils {
    /**
     * 获得两位小数的字符串
     * @param i
     * @return
     */
    public static String getDouble(double i) {
        String num = Double.toString(i);
        if (num.contains(".")) {
            String[] str = num.split("\\.");
            String little = str[1];
            if (little.length() == 1) {
                return num + "0";
            } else if (little.length() == 2) {
                return num;
            } else if (little.length() == 3) {
                return num.substring(0,num.length()-1);
            } else if (little.length() == 4) {
                return num.substring(0,num.length()-2);
            }
            else if (little.length() == 5) {
                return num.substring(0,num.length()-3);
            } else if (little.length() == 6) {
                return num.substring(0,num.length()-4);
            }

        } else {
            return num + ".00";
        }
        return  null;
    }
    /**
     * 获得两位小数的字符串
     * @param num
     * @return
     */
    public static String Str2Double(String num) {
        if (num.contains(".")) {
            String[] str = num.split("\\.");
            String little = str[1];
            if (little.length() == 1) {
                return num + "0";
            } else if (little.length() == 2) {
                return num;
            } else if (little.length() == 3) {
                return num.substring(0,num.length()-1);
            } else if (little.length() == 4) {
                return num.substring(0,num.length()-2);
            }
            else if (little.length() == 5) {
                return num.substring(0,num.length()-3);
            } else if (little.length() == 6) {
                return num.substring(0,num.length()-4);
            }

        } else {
            return num + ".00";
        }
        return  null;
    }
}
